package com.lyh.poi.controller.easyPoi;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.lyh.poi.vo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lyh
 * @date 2022-04-12
 */

@Api(value = "基于注解导入导出",tags = "基于注解导入导出")
@RestController
@RequestMapping("/easyPoi/annotation")
public class ExcelSimpleController {

    /**
     * 导出excel表
     */
    @GetMapping(value = "/out", produces = "application/octet-stream")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "导出用户表")
    public void excel(HttpServletResponse response) {

        List<User> userList = new ArrayList<>();
        for (int i=1; i<=5; i++){
            User user = new User();
            user.setUser_id(i+"");
            user.setPassword("123456"+i);
            user.setUsername("李华"+i);
            user.setPhone("1888633066"+i);
            user.setEmail("19640599"+i+"@qq.com");
            userList.add(user);
        }
        String title = "用户表";
        ExportParams params = new ExportParams(title, "用户数据", ExcelType.HSSF);
        Workbook workbook = ExcelExportUtil.exportExcel(params, User.class, userList);
        ServletOutputStream out = null;
        try {
            response.setHeader("content-type", "application/octet-stream");
            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(title + ".xls", "UTF-8"));
            out = response.getOutputStream();
            workbook.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @ApiOperation("导入用户数据")
    @ApiOperationSupport(order = 2)
    @PostMapping({"/import"})
    public String importExcel(@RequestParam MultipartFile file) {
        ImportParams params = new ImportParams();
        //去掉第一行
        params.setTitleRows(1);
        try {
            List<User> list = ExcelImportUtil.importExcel(file.getInputStream(), User.class, params);
            if (list != null && list.size() > 0) {
                List<User> dataList = list.stream().map((userVo) -> {
                    User user = new User();
                    //属性对拷，可拷贝集合
                    BeanUtils.copyProperties(userVo,user);
                    return user;
                }).collect(Collectors.toList());
                //批量保存到数据库
                boolean b = false;  //TODO 修改批量保存到数据的接口
                if (b) {

                    return "导入成功！";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "导入失败！";
    }

}
